﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XSNManifestRuleSetParseWorker
	{

		public static RuleSet  parseRuleSetByName(Manifest manifest, string ruleSetName) {
			XmlElement root = manifest.Doc.DocumentElement;
			string xsfPrefix = manifest.XsfPrefix;
			
			string ruleSetFilter = "//" + NamespaceUtil.toFullName(xsfPrefix, "ruleSet") + "[@name='" + ruleSetName + "']";
			XmlElement ruleSetElement = (XmlElement) DOMUtil.selectANode(root, ruleSetFilter, manifest.NsResolver);
			if( ruleSetElement == null ){
				return null;
			}

			RuleSet ruleSet = new RuleSet();
			string name = ruleSetElement.GetAttribute("name");
			if (name.Length <= 0) {
				throw new InfoJetException("No ruleSet - name attribute.", "XMRSPW01");
			}
			ruleSet.RuleSetName = name;
			parseRules(manifest, ruleSetElement, ruleSet);
			
			return ruleSet;
		}
		
		private static void  parseRules(Manifest manifest, XmlElement ruleSetElement, RuleSet ruleSet)
		{
			string xsfPrefix = manifest.XsfPrefix;
			
			ArrayList ruleList = new ArrayList();
			
			string ruleFilter = NamespaceUtil.toFullName(xsfPrefix, "rule");
			XmlNodeList ruleXmlNodeList = DOMUtil.selectNodes(ruleSetElement, ruleFilter, 0, manifest.NsResolver);
			foreach ( XmlNode ruleXmlNode in ruleXmlNodeList )
			{
				XmlElement ruleElement = (XmlElement) ruleXmlNode;
				Rule rule = new Rule();
				string caption = ruleElement.GetAttribute("caption");
				if (caption.Length <= 0)
				{
					throw new InfoJetException("No rule - caption attribute.", "XMRSPW02");
				}
				rule.Caption = caption;
				string condition = ruleElement.GetAttribute("condition");
				if (condition.Length <= 0)
				{
					condition = "true()";
				}
				rule.Condition = condition;
				string enabled = ruleElement.GetAttribute("isEnabled");
				if (enabled.Equals("no"))
				{
					rule.Enabled = false;
				}

				ArrayList ruleActionList = new ArrayList();
				XmlNodeList childList = ruleElement.ChildNodes;
				foreach ( XmlNode childNode in childList )
				{
					if (childNode is XmlElement)
					{
						XmlElement actionElement = (XmlElement)childNode;
						RuleAction ruleAction = parseRuleAction( manifest, actionElement, xsfPrefix);
						if (ruleAction != null)
						{
							if (ruleAction.RuleActionType == 0)
							{
								rule.ExitRuleSet = true;
							}
							else
							{
								ruleActionList.Add(ruleAction);
							}
						}
					}
				}
				rule.RuleActionList = ruleActionList;
				ruleList.Add(rule);
			}
			
			ruleSet.RuleList = ruleList;
		}
		
		private static RuleAction parseRuleAction( Manifest manifest, XmlElement actionElement, string xsfPrefix)
		{
			if (actionElement.Name.Equals(NamespaceUtil.toFullName(xsfPrefix, "dialogBoxMessageAction"))) 
			{
				RuleAction ruleAction = new RuleAction();
				ruleAction.RuleActionType = RuleAction.iDialogBoxMessage;
				string message = actionElement.InnerText;
				ruleAction.DialogBoxMessage = message;
				return ruleAction;
			}
			else if (actionElement.Name.Equals(NamespaceUtil.toFullName(xsfPrefix, "dialogBoxExpressionAction"))) 
			{
				RuleAction ruleAction = new RuleAction();
				ruleAction.RuleActionType = RuleAction.iDialogBoxExpression;
				ruleAction.DialogBoxExpression = actionElement.InnerText;
				return ruleAction;
			}
			else if (actionElement.Name.Equals(NamespaceUtil.toFullName(xsfPrefix, "assignmentAction"))) 
			{
				RuleAction ruleAction = new RuleAction();
				ruleAction.RuleActionType = RuleAction.iAssignment;
				string targetField = actionElement.GetAttribute("targetField");
				if (targetField.Length <= 0) 
				{
					throw new InfoJetException("No assignmentAction - targetField attribute.", "XMRSPW04");
				}
				ruleAction.AssignmentTargetField = targetField;
				string expression = actionElement.GetAttribute("expression");
				if (expression.Length <= 0) 
				{
					throw new InfoJetException("No assignmentAction - expression attribute.", "XMRSPW05");
				}
				ruleAction.AssignmentExpression = expression;
				return ruleAction;
			}
			else if (actionElement.Name.Equals(NamespaceUtil.toFullName(xsfPrefix, "exitRuleSet"))) 
			{
				RuleAction ruleAction = new RuleAction();
				ruleAction.RuleActionType = RuleAction.iExitRuleSet;
				return ruleAction;
			}
			else if(actionElement.Name.Equals(NamespaceUtil.toFullName(xsfPrefix, "switchViewAction")))
			{
				RuleAction ruleAction = new RuleAction();
				ruleAction.RuleActionType = RuleAction.iSwitchView;
				string viewName = actionElement.GetAttribute("view");
				if( viewName.Length <= 0 )
				{
					throw new InfoJetException("No switchViewAction - view attribute.", "XMRSPW06");
				}
                //CID 1704 //2013.04.26 //Begin
                if (manifest.ViewList != null)
                {
                    foreach (View view in manifest.ViewList)
                    {
                        if (view.Name.ToLower().Equals(viewName.ToLower()))
                        {
                            ruleAction.SwitchViewId = view.Id;
                        }
                    }
                    if (ruleAction.SwitchViewId == null)
                    {
                        throw new InfoJetException("Cannot find switchView: " + viewName, "XMSRSPW07");
                    }
                }
                else
                {
                    ruleAction.SwitchViewId = viewName;
                }
                //CID 1704 //2013.04.26 //End
				return ruleAction;
			}
			else if(actionElement.Name.Equals(NamespaceUtil.toFullName(xsfPrefix, "submitAction")))
			{
				RuleAction ruleAction = new RuleAction();
				ruleAction.RuleActionType = RuleAction.iSubmitAction;
				string adapter = actionElement.GetAttribute( "adapter" );
				if( adapter.Length <= 0 )
				{
					throw new InfoJetException("Cannot find submitAction adapter.", "XMSRSPW08");
				}
				else
				{
					ruleAction.SubmitDataAdapterName = adapter;
					object dataAdapter = XSNManifestDataAdapterParseWorker.findDataAdapterByName( manifest, ruleAction.SubmitDataAdapterName );
					if( dataAdapter != null )
					{
						ruleAction.SubmitDataAdapter = dataAdapter;
						return ruleAction;
					}
				}
			}
			else if(actionElement.Name.Equals(NamespaceUtil.toFullName(xsfPrefix, "closeDocumentAction")))
			{
				RuleAction ruleAction = new RuleAction();
				ruleAction.RuleActionType = RuleAction.iCloseDocAction;
				string promptToSaveChanges = actionElement.GetAttribute( "promptToSaveChanges" );
				if( promptToSaveChanges.Equals( "yes" ) ) 
				{
					ruleAction.PromptToSaveChanges = true;
				}
				else
				{
					ruleAction.PromptToSaveChanges = false;
				}
				return ruleAction;
			}
			else if(actionElement.Name.Equals(NamespaceUtil.toFullName(xsfPrefix, "queryAction")))
			{
				RuleAction ruleAction = new RuleAction();
				ruleAction.RuleActionType = RuleAction.iQueryAction;
				ruleAction.QueryDataObjectName = actionElement.GetAttribute( "adapter" );
				return ruleAction;
			}
			//CID 0369 //2009.07.18 //Begin
			else if(actionElement.Name.Equals(NamespaceUtil.toFullName(xsfPrefix, "openNewDocumentAction")))
			{
				RuleAction ruleAction = new RuleAction();
				ruleAction.RuleActionType = RuleAction.iOpenNewDocumentAction;
				ruleAction.SolutionURI = actionElement.GetAttribute( "solutionURI" );
				return ruleAction;
			}
			//CID 0369 //2009.07.18 //End
            //CID 0604 //2010.06.10 //Begin
            else if (actionElement.Name.Equals(NamespaceUtil.toFullName(xsfPrefix, "changeAdapterProperty")))
            {
                RuleAction ruleAction = new RuleAction();
                ruleAction.RuleActionType = RuleAction.iChangeAdapterProperty;
                ruleAction.Adapter = actionElement.GetAttribute("adapter");
                ruleAction.AdapterProperty = actionElement.GetAttribute("adapterProperty");
                ruleAction.Expression = actionElement.GetAttribute("expression");
                return ruleAction;
            }
            //CID 0604 //2010.06.10 //End
			return null;
		}

        //CID 1704 //2013.04.26 //Begin
        public static void postParseSubmitSwitchViews(Manifest manifest) 
        {
            if (manifest.Submit != null)
            {
                if (manifest.Submit.RuleSet != null)
                {
                    foreach (Rule rule in manifest.Submit.RuleSet.RuleList)
                    {
                        foreach (RuleAction ruleAction in rule.RuleActionList)
                        {
                            //2013.4.26 添加判断ruleAction是否为切换试图 by Sunny
                            if (!string.IsNullOrEmpty(ruleAction.SwitchViewId))
                            {
                                int viewId = -1;
                                bool isViewId = int.TryParse(ruleAction.SwitchViewId, out viewId);
                                if (isViewId && viewId >= 0)
                                {
                                    //OK.
                                }
                                else
                                {
                                    string viewName = ruleAction.SwitchViewId;
                                    foreach (View view in manifest.ViewList)
                                    {
                                        if (view.Name.ToLower().Equals(viewName.ToLower()))
                                        {
                                            ruleAction.SwitchViewId = view.Id;
                                        }
                                    }
                                    if (ruleAction.SwitchViewId == null)
                                    {
                                        throw new InfoJetException("Cannot find switchView: " + viewName, "XMSRSPW07");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        //CID 1704 //2013.04.26 //End
	}
}